/**
 * gcog - generate code with inlined Groovy code.
 * http://code.google.com/p/gcog/
 * 
 * Copyright 2008, Tobias Hientzsch.
 * 
 * MIT license http://www.opensource.org/licenses/mit-license.php
 * 
 * Original idea and code from Cog code generation tool.
 * http://nedbatchelder.com/code/cog
 *
 * Copyright 2004-2008, Ned Batchelder.
 * 
 */

import junit.framework.TestCase

import static Makefiles.*
import static WhiteUtils.*


/**
 * @author tobias
 *
 */
public class GCogTest extends GroovyTestCase{
	void test1 () {
		def c = new GCog()
		assert c.isBeginSpecLine("[[[gcog bla")
		assert c.isEndSpecLine("]]]")
		assert c.isEndOutputLine("[[[end]]]")
	}
	
	void testReadLine() {
		def r = new NumberedFileReader(new StringReader("bla\nfasel"))
		assertEquals("bla", r.readLine())
		assertEquals("fasel", r.readLine())
	}

	void failsReadLine2() {
		def r = new NumberedFileReader(new StringReader("bla\nfasel\n"))
		assertEquals("bla", r.readLine())
		assertEquals("fasel", r.readLine())
		assertEquals("", r.readLine())
	}

    void testNoCog() {
    	def	strings = [
            '',
            ' ',
            ' \t \t \tx',
            'hello',
            'the cat\nin the\nhat.',
            'Horton\n\tHears A\n\t\tWho'
            ]
        strings.each { s ->
            assertEquals(new GCog().processString(s), s)
    	}
    }

    void testSimple0() {
        def infile = """\
            Some text.
            //[[[gcog
            outl("This is line one")
            //]]]

            gobbledegook.
            //[[[end]]]
            epilogue.
            """

        def outfile = """\
            Some text.
            //[[[gcog
            outl("This is line one")
            //]]]
            This is line one
            //[[[end]]]
            epilogue.
            """

        assertEquals(outfile, new GCog().processString(infile))
    }

    void testSimple() {
        def infile = """\
            Some text.
            //[[[gcog
            import java.io.*
            outl("This is line one\\n")
            outl("This is line two")
            //]]]
            gobbledegook.
            //[[[end]]]
            epilogue.
            """

        def outfile = """\
            Some text.
            //[[[gcog
            import java.io.*
            outl("This is line one\\n")
            outl("This is line two")
            //]]]
            This is line one

            This is line two
            //[[[end]]]
            epilogue.
            """

        assertEquals(outfile, new GCog().processString(infile))
    }

    void testEmptyCog() {
        // The cog clause can be totally empty.  Not sure why you'd want it,
        // but it works.
        def infile = """\
        	hello
        	//[[[gcog
        	//]]]
        	//[[[end]]]
        	goodbye """

        infile = reindentBlock(infile)
        assertEquals(infile, new GCog().processString(infile))
    }

    def assertFilesSame(sFName1, sFName2) {
        assertEquals(new File(sFName1).readLines(), new File(sFName2).readLines())
    }

    void testSimpleFile() {
    	def d = [
                'test.cog': """\
                    first line
                    hey: [[[gcog outl("hello") ]]] looky!
                    get rid of this!
                    [[[end]]]
                    last line
                    """,
                
                'test.out.exp': """\
                    first line
                    hey: [[[gcog outl("hello") ]]] looky!
                    hello
                    [[[end]]]
                    last line
                    """]
    	makefiles(d)
    	new GCog().processFile("test.cog", "test.out", "test.cog")
    	assertFilesSame("test.out.exp", "test.out")
    }
    
    void testSameFile() {
    	def d = [
                'test.cog': """\
                    first line
                    hey: [[[gcog cog.outl("hello") ]]] looky!
                    get rid of this!
                    [[[end]]]
                    last line
                    """,
                
                'test.out.exp': """\
                    first line
                    hey: [[[gcog cog.outl("hello") ]]] looky!
                    hello
                    [[[end]]]
                    last line
                    """]
    	makefiles(d)
    	//new GCog().processOneFile("test.cog", "test.cog")
    	GCog.main(["test.cog"] as String[])
    	assertFilesSame("test.out.exp", "test.cog")
    }
}
